package Network;



import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import Controler.Controler;
import Model.PropertiesUtils;

public class Client implements Runnable {
	// The client socket
	private Object parent = null;
	private Socket socket = null;
	// The output stream
	// private PrintStream os = null;
	// The input stream
	// private DataInputStream is = null;
	private Thread thread;
	private ObjectOutputStream oos;
	private ObjectInputStream ios;
	// private BufferedReader inputLine = null;
	private boolean closed = false;

	public Client(Object parent, String host, int port) {

		this.parent = parent;

		/*
		 * Open a socket on a given host and port. Open input and output
		 * streams.
		 */
		try {
			socket = new Socket(host, port);
			oos = new ObjectOutputStream(socket.getOutputStream());
			ios = new ObjectInputStream(socket.getInputStream());
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host " + host);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for the client to server connection to the host "
							+ host);
		}

		/*
		 * If everything has been initialized then we want to write some data to
		 * the socket we have opened a connection to on the port port.
		 */
		if (socket != null && ios != null && oos != null
				&& socket.isConnected()) {

			/* Create a thread to read from the server. */
			thread = new Thread(this);
			thread.start();
			/*
			 * Close the output stream, close the input stream, close the
			 * socket.
			 */

		}
	}

	/*
	 * Create a thread to read from the server. (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	/*
	 * public void send(FormDrawable form){ if(oos!= null){ try {
	 * 
	 * oos.flush(); oos.reset(); oos.writeObject(form); } catch (IOException e)
	 * { e.printStackTrace(); } }
	 * 
	 * }
	 */
	public void run() {
		/*
		 * Keep on reading from the socket till we receive "Bye" from the
		 * server. Once we received that then we want to break.
		 */
		if(parent instanceof Controler){
			((Controler)parent).connectedToHost();
		}
		while (!closed) {
			// FormDrawable temp;
			
			Object rcvObj;
			try {
				rcvObj = ios.readObject();
				if (rcvObj != null) {
					
					if(parent!=null)
						if(parent instanceof Controler)
							((Controler) parent).receive(rcvObj);
					/*
					 * if(temp instanceof FormDrawable){
					 * refPane.updateDrawable((FormDrawable)temp);
					 * System.out.println((FormDrawable)temp); } else if(temp
					 * instanceof LinkedList){ listeClients =
					 * (LinkedList<Client>)temp;
					 * System.out.println(listeClients); }
					 */

				}
				// closed = true;
			} catch (EOFException e) {
				System.err.println("EOF:  " + e);
				try {
					oos.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				closed = true;
				// ((Fenetre)this.getParent()).clientSocket = null;
				// ((Fenetre)this.getParent()).listener.setClientSocket(null);

			} catch (ClassNotFoundException e) {
				System.err.println("ClassNotFoundException: " + e);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public Object getParent() {
		return parent;
	}

	public void setParent(Object parent) {
		this.parent = parent;
	}
	
	public void send(Object object)
	  {
      	try {
      		oos.flush();
      		oos.reset();
      		oos.writeObject(object);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	  }
}